package com.kgcx.organization.service;

import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.AutoBrand;
import com.kgcx.organization.mapper.AutoBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AutoBrandService {
    @Autowired
    private AutoBrandMapper autoBrandMapper;

    public AutoBrand getAutoBrand(Long kid) {
        return autoBrandMapper.selectByPrimaryKey(kid);
    }

    public List<AutoBrand> getAutoBrandList(RequestData rd) {
        PageUtils.page(rd);
        return autoBrandMapper.selectAllActive();
    }

    @Transactional(rollbackFor = Exception.class)
    public void addAutoBrand(AutoBrand autoBrand) {
        autoBrandMapper.insertSelective(autoBrand);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateAutoBrand(AutoBrand autoBrand) {
        autoBrandMapper.updateByPrimaryKeySelective(autoBrand);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeAutoBrand(Long kid) {
        AutoBrand autoBrand = new AutoBrand();
        autoBrand.setKid(kid);
        autoBrand.setStatus(Cons.COMMON_STATUS_DELETE);
        autoBrandMapper.updateByPrimaryKeySelective(autoBrand);
    }

    public String getBrandNameById(Long kid) {
        AutoBrand autoBrand = getAutoBrand(kid);
        if (null != autoBrand) {
            return autoBrand.getName();
        }
        return null;
    }
}
